width = 32  ;%宽度
depth = 100 ;%深度，时钟频率/采样点=波形频率
Amp   = 1   ;%浮点幅度
x = linspace(0,2*pi,depth);

if(mod(width,4) == 0)
    N = width/4;
else
    N = int32(width/4) + 1;
end

%% 无符号
y = sin(x);
y = y * ((2^(width-1)) - 1) + ((2^(width-1) - 1));
y = uint32(y);
subplot(2,2,1);
plot(y);

fid = fopen('.\unsignwav.coe','w');
fprintf(fid,"memory_initialization_radix = 10; \n");
fprintf(fid,"memory_initialization_vector = \n");

for i = 1:depth
    if(i == depth)
        fprintf(fid,"%d;\n",dec2hex(y(i),N));
    else
        fprintf(fid,"%d,\n",dec2hex(y(i),N));
    end
end

fclose(fid);

%% float浮点
fy = Amp*sin(x);
subplot(2,2,3);
plot(fy)
fy = num2hex(single(fy));

fid = fopen('.\floatwav.coe','w');
fprintf(fid,"memory_initialization_radix = 16; \n");
fprintf(fid,"memory_initialization_vector = \n");

for i = 1:depth
    fprintf(fid,"%s;\n",fy(i,:));
end

fclose(fid);

%% double浮点
dy = Amp*sin(x);
subplot(2,2,4);plot(dy)
dy = num2hex(dy);

fid = fopen('.\doublewav.coe','w');
fprintf(fid,"memory_initialization_radix = 16; \n");
fprintf(fid,"memory_initialization_vector = \n");

for i = 1:depth
    fprintf(fid,"%s;\n",dy(i,:));
end

fclose(fid);